<?php

class productPageActions extends myFrontendAction
{
  public function preExecute()
  {
    parent::preExecute();
    
    $this->product  = $this->getRoute()->getProductObject();
    $this->category = $this->getRoute()->getCategoryObject();
    $this->matrix   = Doctrine::getTable('Matrix');
    $this->table    = Doctrine::getTable('Parameter');
  }


  /**
   * Default product page
   */
  public function executeIndex(sfWebRequest $request)
  {
    $parameter = new Parameter;
    
    // Whether we need to fetch parameter value
    if($request->getParameter('id', 0) > 0) {
      $this->value = $parameter->getValueById($request->getParameter('id', 0), $this->getUser());
      
      if($this->value) {
        $this->active = $request->getParameter('id', 0);
      } else $this->active = false;
    }
    
    // Fetching parameters for tabs
    $this->tabs = $parameter->getConnectedParams(
      $this->product->getId(),
      $this->getUser()
    );
    
    // Fetching product pictures (used by fancybox)
    $this->productPictures = Doctrine::getTable('ProductPictures')->getProductPictures(
      $this->getUser(),
      $this->product->getId()
    );
    
    if($request->getParameter('act', 'lahendus') == 'lahendus') {
      $this->lahendus($request);
    }
  }
  

  public function executePrint(sfWebRequest $request)
  {
    $this->setLayout(false);
  }

  /**
   * Koosta lahendus section (external invoked)
   *
   * @param sfWebRequest $request
   */
  public function lahendus(sfWebRequest $request)
  {
    $this->groups    = $this->table->getLahenduseGroups($this->category->getId(), $this->getUser());
    $this->parameter = false;

    if($this->groups) {
      // Fetch first parameters related to the top groups
      $this->parameter = $this->table->getFirstParameter($this->groups[0]['ParameterGroup']['id'], $this->getUser());
    }
  }
  
  public function executeAjax(sfWebRequest $request)
  {
    $this->setLayout(false);

    if($request->isXmlHttpRequest()) {
      $this->parameters = $this->matrix->getParameters($request, $this->getUser());
    } else {
      return sfView::NONE;
    }
  }
}